package com.qkl.xiatang.app.service.wvp;

import com.qkl.xiatang.app.dto.BusinessDTO;
import com.qkl.xiatang.app.dto.PageOutDTO;
import com.qkl.xiatang.app.dto.wvp.*;
import com.qkl.xiatang.app.mapper.WvpMapper;
import com.qkl.xiatang.app.service.pc.PCService;
import com.qkl.xiatang.app.utils.*;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class WvpService {

    @Autowired
    private WvpMapper wvpMapper;

    @Autowired
    private WvpChannelService wvpChannelService;

    @Autowired
    private PCService pcService;


    /**
     * 监控查询
     * @param dto
     * @return
     */
    public BusinessDTO<PageOutDTO<MonitorQueryOutDTO>> queryMonitorList(MonitorQueryInDTO dto){
        String companyId = AppSessionUtils.getCompanyId();
        dto.setCompanyId(companyId);
        dto.checkParam();

        wvpChannelService.channelRefresh(companyId);

        Integer total = wvpMapper.queryMonitorTotal(dto);
        total = null == total ? 0 : total;
        PageOutDTO<MonitorQueryOutDTO> outDTO = new PageOutDTO<>();
        outDTO.settingPage(dto,total);
        if(total > 0){
            outDTO.setList(wvpMapper.queryMonitorList(dto));
        }
        if(ObjectUtils.isEmpty(outDTO.getList())){
            outDTO.setList(new ArrayList<>());
        }else {
            List<MonitorQueryOutDTO> list = outDTO.getList();
            for (MonitorQueryOutDTO queryOutDTO : list){
                queryOutDTO.trimEmpty();
            }
        }
        return BusinessDTO.sucessBusiness(outDTO,"监控查询");
    }

    /**
     * 监控明细查询
     * @param dto
     * @return
     */
    public BusinessDTO<MonitorQueryOutDTO> queryMonitorDetail(MonitorQueryInDTO dto){
        String companyId = AppSessionUtils.getCompanyId();
        dto.setCompanyId(companyId);
        dto.checkParam();
        List<MonitorQueryOutDTO> detailList = wvpMapper.queryMonitorList(dto);
        if(ObjectUtils.isEmpty(detailList)){
            return BusinessDTO.failBusiness("查询无数据");
        }
        MonitorQueryOutDTO outDTO = detailList.get(0);
        outDTO.trimEmpty();
        return BusinessDTO.sucessBusiness(outDTO,"监控明细查询");
    }

    /**
     * 通用前端控制命令
     * @param dto
     * @return
     */
    public BusinessDTO<Object> controlFrontEnd(ControlFrontEndDTO dto){
        Result result = pcService.controlFrontEnd(dto);
        if(result.resultSuccess()){
            return BusinessDTO.sucessBusiness(new HashMap<>(),"通用前端控制命令");
        }
        return BusinessDTO.failBusiness(result.getCode(),result.getMessage());
    }

    /**
     * 云台控制
     * @param dto
     * @return
     */
    public BusinessDTO<Object> controlCloud(ControlCloudDTO dto){
        Result result = pcService.controlCloud(dto);
        if(result.resultSuccess()){
            return BusinessDTO.sucessBusiness(new HashMap<>(),"云台控制");
        }
        return BusinessDTO.failBusiness(result.getCode(),result.getMessage());
    }

    /**
     * 视频回放
     * @param playBack
     * @return
     */
    public BusinessDTO<Object> playBack(IPlayBack playBack){
        Result result = null;
        if( playBack instanceof PlayBackStartDTO){
            result = pcService.playBack((PlayBackStartDTO)playBack);
            toJavaResult(result);
        }else if( playBack instanceof PlayBackStopDTO){
            result = pcService.playBack((PlayBackStopDTO)playBack);
        }else if( playBack instanceof PlayBackSpeedDTO){
            result = pcService.playBack((PlayBackSpeedDTO)playBack);
        }else if( playBack instanceof PlayBackSeekDTO){
            result = pcService.playBack((PlayBackSeekDTO)playBack);
        }else if( playBack instanceof PlayBackResumeDTO){
            result = pcService.playBack((PlayBackResumeDTO)playBack);
        }else if( playBack instanceof PlayBackPauseDTO){
            result = pcService.playBack((PlayBackPauseDTO)playBack);
        }else if( playBack instanceof PlayBackRecordQueryDTO){
            result = pcService.playBack((PlayBackRecordQueryDTO)playBack);
        }else {
            return BusinessDTO.failBusiness("不支持的请求");
        }
        if(result.resultSuccess()){
            return BusinessDTO.sucessBusiness(result.getData(),"视频回放");
        }
        return BusinessDTO.failBusiness(result.getCode(),result.getMessage());
    }

    private static void toJavaResult(Result result){
        if(null == result || !result.resultSuccess() || null == result.getData() || !(result.getData() instanceof Map)){
            return ;
        }
        Map<String,Object> dataMap = new HashMap<>();
        Map<String,Object> data = (Map<String,Object>)result.getData();
        Set<String> keys = data.keySet();
        for (String key : keys){
            if("tracks".equals(key)){
                continue;
            }
            Object value = data.get(key);
            if( null == value){
                value = "";
            }
            dataMap.put(AppUtils.toJavaName(key),value);
        }
        result.setData(dataMap);
    }

}
